package com.example.mumu.passwordbook.common;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mumu.passwordbook.R;
import com.example.mumu.passwordbook.domain.Account;

import java.util.List;

public class AccountAdapter extends BaseAdapter {

    private final List<Account> accountList;
    private final Context context;

    public AccountAdapter(Context context ,List<Account> accountList) {
        this.accountList = accountList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return accountList.size();
    }

    @Override
    public Account getItem(int position) {
        return accountList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = View.inflate(context,
                    R.layout.item_list_account, null);
            new ViewHolder(convertView);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        Account account = getItem(position);
        holder.iv_icon.setImageDrawable(context.getResources().getDrawable(account.getIcon().getResource()));
        holder.tv_name.setText(account.getName());
        holder.iv_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "iv_icon_click", Toast.LENGTH_SHORT).show();
            }
        });
        holder.tv_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"tv_name_click",Toast.LENGTH_SHORT).show();
            }
        });
        holder.iv_view.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "iv_view_click", Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView iv_icon;
        TextView tv_name;
        ImageView iv_view;


        public ViewHolder(View view) {
            iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            iv_view = (ImageView) view.findViewById(R.id.iv_view);
            view.setTag(this);
        }
    }
}
